package com.honey.compilation.sql;

import com.honey.core.builder.CharacterBuilder;
import com.honey.core.builder.CharacterBuilderFactory;
import com.honey.core.compiler.CompilationElement;
import com.honey.core.compiler.CompilationException;
import com.honey.core.compiler.IndentSpace;
import com.honey.core.utils.StringUtility;

/**
 * &lt;table name&gt;    ::=   &lt;qualified name&gt; | &lt;qualified local table name&gt;<br />
 * &lt;qualified name&gt;    ::=   [ &lt;schema name&gt; &lt;period&gt; ] &lt;qualified identifier&gt;<br />
 * 
 * @author Administrator
 *
 */
public class SqlTable implements CompilationElement {
	
	/** 表所属的数据库结构 */
	private String schema;
	
	/** 表的名称 */
	private ValueElement tableName ;

	/**
	 * 构造函数
	 * @param tableName 表名称
	 */
	public SqlTable(String tableName ){
		this(null, tableName);
	}
	
	/**
	 * 构造函数
	 * @param schema 表所属的schema
	 * @param tableName 表的名称
	 */
	public SqlTable(String schema ,String tableName ){
		this.tableName = new ValueElement( tableName );
		this.setSchema(schema);
	}
	
	/**
	 * 
	 * 获取schema
	 * @return
	 */
	public String getSchema() {
		return schema;
	}

	/**
	 * 设定数据库schema
	 * @param schema
	 */
	public void setSchema(String schema) {
		if(! StringUtility.stringHasValue(schema)){
			this.schema = null ;
		}else{
			this.schema = schema;
		}
	}
	
	/**
	 * 获取表的名称
	 * @return
	 */
	public String getTableName() {
		if(tableName== null)
			return null;
		return tableName.getValue();
	}

	/*
	 * 
	 * (non-Javadoc)
	 * @see com.honey.core.dom.CompilationElement#compiledContent(int)
	 */
	@Override
	public CharacterBuilder compiledContent(int indentLevel ) throws CompilationException {
//		CharacterBuilder answer = CharacterBuilderFactory.createC8StringBuilder();
//		if( ! StringUtility.stringHasValue( this.tableName.getValue()) )
//			throw new CompilationException("没有找到数据库表名称,请先转入表的名称.");
//		
//		if(tableName != null ){
//			IndentSpace.spaceIndent(answer, indentLevel);
//			if(schema != null ){
//				answer.append(schema).append(SqlKeyWord.OPERATION_PERIOD);
//			}
//			answer.append(tableName);
//		}
		return compiledContent(indentLevel, true);
	}
	
	/**
	 * 
	 * @param indentLevel
	 * @param space
	 * @return
	 * @throws CompilationException
	 */
	public CharacterBuilder compiledContent(int indentLevel,boolean space ) throws CompilationException {
		CharacterBuilder answer = CharacterBuilderFactory.createC8StringBuilder();
		if( ! StringUtility.stringHasValue( this.tableName.getValue()) )
			throw new CompilationException("没有找到数据库表名称,请先转入表的名称.");
		
		if(tableName != null ){
			IndentSpace.spaceIndent(answer, indentLevel);
			if(schema != null ){
				answer.append(schema).append(SqlKeyWord.OPERATION_PERIOD);
			}
			
			if(space)
				answer.append(tableName);
			else
				answer.append(tableName.getValue());
		}
		return answer;
	}
	
	
	@Override
	public String toString() {
		try {
			return compiledContent(0).toString();
		} catch (CompilationException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 数据库比较 
	 */
	@Override
	public boolean equals(Object obj) {
		if( obj instanceof SqlTable ){
			SqlTable srcTable = (SqlTable)obj ;
			CharacterBuilder src = CharacterBuilderFactory.createC16StringBuilder();
			
			if(  StringUtility.stringHasValue( srcTable.getTableName() ) ){
				if( StringUtility.stringHasValue( srcTable.getSchema()  ) ){
					src.append( srcTable.getSchema());
				}
				src.append( srcTable.getTableName());
			}else{
				return super.equals(obj) ;
			}
			
			CharacterBuilder des = CharacterBuilderFactory.createC16StringBuilder();
			if( StringUtility.stringHasValue( this.getTableName() ) ){
				if( StringUtility.stringHasValue( this.getSchema()  ) ){
					des.append( this.getSchema());
				}
				des.append( this.getTableName());
			}else{
				return super.equals(obj) ;
			}
			
			return des.toString().equals(src.toString() ) ;
		}else{
			return super.equals(obj);
		}
	}
	
	@Override
	public int hashCode() {
		CharacterBuilder des = CharacterBuilderFactory.createC16StringBuilder();
		if( StringUtility.stringHasValue( this.getTableName() ) ){
			if( StringUtility.stringHasValue( this.getSchema()  ) ){
				des.append( this.getSchema());
			}
			des.append( this.getTableName());
		}else{
			return super.hashCode();
		}
		return des.toString().hashCode();
	}
}
